package com.winchain.chainbill.bill.biz.transfer.rx;

import com.shell.bee.acct.transaction.po.AcctTxRec;
import com.shell.framework.command.CallbackContext;
import com.shell.framework.command.ChainCallback;
import com.shell.framework.rx.OnErrorNotImplementedSubscriber;
import com.winchain.chainbill.bill.base.po.CbOpt;
import com.winchain.chainbill.bill.base.po.CbXfe;
import com.winchain.chainbill.bill.biz.Bill;
import com.winchain.chainbill.bill.biz.BillTranEvent;
import com.winchain.chainbill.bill.biz.receive.cmd.BillHldrCmd;
import com.winchain.chainbill.bill.biz.receive.cmd.BillRcvDfrlCmd;
import com.winchain.chainbill.bill.biz.send.cmd.BillMoveCmd;
import com.winchain.chainbill.bill.biz.send.cmd.BillOptCmd;
import com.winchain.chainbill.bill.biz.transfer.cmd.BillRcv4PrntCmd;
import com.winchain.chainbill.bill.biz.transfer.cmd.BillRcv4XfeCmd;
import com.winchain.chainbill.bill.biz.transfer.cmd.BillXfeCertCmd;
import com.winchain.chainbill.bill.biz.transfer.cmd.BillXfeCmd;
import com.winchain.chainbill.bill.biz.transfer.cmd.BillXfeMsgCmd;
import com.winchain.chainbill.trade.cmd.TranCallbackCmd;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class BillXfeRevCallbackChdSubscriber extends OnErrorNotImplementedSubscriber<BillTranEvent<CbXfe>> {
	@Autowired
	private BillOptCmd<CbXfe> billOptCmd;
	@Autowired
	private BillXfeCmd billXfeCmd;
	@Autowired
	private BillMoveCmd<CbXfe> billMoveCmd;
	@Autowired
	private BillRcv4XfeCmd billRcv4TransferCmd;
	@Autowired
	private BillRcv4PrntCmd<CbXfe> billRcv4PrntCmd;
	@Autowired
	private BillRcvDfrlCmd<CbXfe> billDfrlCmd;
	@Autowired
	private BillHldrCmd<CbXfe> billHldrCmd;
	@Autowired
	private BillXfeCertCmd billXfeCertCmd;
	@Autowired
	private BillXfeMsgCmd billXfeMsgCmd;
	@Autowired
	private TranCallbackCmd<Bill<CbXfe>> tranCallbackCmd;

	public void onApplicationEvent(BillTranEvent<CbXfe> t) {
		List<Bill<CbXfe>> child = (List<Bill<CbXfe>>) t.getChildren();
		AcctTxRec tran = t.getTran();
		for (Bill<CbXfe> bill : child) {
			CbOpt opt = new CbOpt();
			if (((CbXfe) bill.getSource()).getXfeId() == null) {
				opt.setCurState("S000");
				opt.setOptCode("E502");
				opt.setRemark("����������������������������");
			} else {
				opt.setCurState("S000");
				opt.setOptCode("E501");
				opt.setRemark("����������������������������");
			}
			CallbackContext<Bill<CbXfe>, AcctTxRec> context = new CallbackContext();
			context.setSource(bill);
			context.setTarget(tran);
			context.setValue("Operation", opt);
			context.setValue("bizType", "cb_xfe_rev");

			ChainCallback<Bill<CbXfe>, AcctTxRec> ccb = new ChainCallback();
			ccb.add(this.billOptCmd);
			if (((CbXfe) bill.getSource()).getXfeId() == null) {
				String buyerId = null;
				String buyerName = null;
				for (Bill<CbXfe> billChd : child) {
					if (((CbXfe) billChd.getSource()).getXfeId() != null) {
						buyerId = ((CbXfe) billChd.getSource()).getBuyerId();
						buyerName = ((CbXfe) billChd.getSource()).getBuyerNm();
						break;
					}
				}
				context.setValue("OwnerId", buyerId);
				context.setValue("OwberName", buyerName);
				ccb.add(this.billHldrCmd);
				ccb.add(this.billRcv4PrntCmd);

				ccb.add(this.billXfeCertCmd);
			}
			if (((CbXfe) bill.getSource()).getXfeId() != null) {
				ccb.add(this.billXfeCmd);
				ccb.add(this.billDfrlCmd);
				ccb.add(this.billRcv4TransferCmd);
				ccb.add(this.tranCallbackCmd);
				ccb.add(this.billXfeMsgCmd);
			}
			ccb.add(this.billMoveCmd);

			ccb.callback(context);
		}
	}
}
